package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordAttend;
import com.ruoyi.record.service.IRecordAttendService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤Controller
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Controller
@RequestMapping("/record/attend")
public class RecordAttendController extends BaseController
{
    private String prefix = "record/attend";

    @Autowired
    private IRecordAttendService recordAttendService;

    @RequiresPermissions("record:attend:view")
    @GetMapping()
    public String attend()
    {
        return prefix + "/attend";
    }

    /**
     * 查询考勤列表
     */
    @RequiresPermissions("record:attend:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordAttend recordAttend)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()) {
            recordAttend.setDeptId(ShiroUtils.getDeptId());
        }
        List<RecordAttend> list = recordAttendService.selectRecordAttendList(recordAttend);
        return getDataTable(list);
    }

    /**
     * 新增考勤
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("dept", ShiroUtils.getSysUser().getDept());
        mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/add";
    }

    /**
     * 新增保存考勤
     */
    @RequiresPermissions("record:attend:add")
    @Log(title = "考勤", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordAttend recordAttend)
    {
        if(recordAttendService.checkRecordAttend(recordAttend)) {
            recordAttend.setCreateBy(ShiroUtils.getLoginName());
            return toAjax(recordAttendService.insertRecordAttend(recordAttend));
        }else {
            return error("当前月份已存在考勤记录");
        }
    }

    /**
     * 修改考勤
     */
    @GetMapping("/edit/{attendId}")
    public String edit(@PathVariable("attendId") Long attendId, ModelMap mmap)
    {
        RecordAttend recordAttend = recordAttendService.selectRecordAttendByAttendId(attendId);
        mmap.put("recordAttend", recordAttend);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤
     */
    @RequiresPermissions("record:attend:edit")
    @Log(title = "考勤", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordAttend recordAttend)
    {
        if(recordAttendService.checkRecordAttend(recordAttend)) {
            recordAttend.setUpdateBy(ShiroUtils.getLoginName());
            return toAjax(recordAttendService.updateRecordAttend(recordAttend));
        }else{
            return error("当前月份已存在考勤记录");
        }
    }

    /**
     * 删除考勤
     */
    @RequiresPermissions("record:attend:remove")
    @Log(title = "考勤", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordAttendService.deleteRecordAttendByAttendIds(ids));
    }

    /**
     * 提交考勤记录
     */
    @RequiresPermissions("record:attend:submit")
    @Log(title = "考勤", businessType = BusinessType.UPDATE)
    @PostMapping( "/submit")
    @ResponseBody
    public AjaxResult submit(String attendIds)
    {
        return toAjax(recordAttendService.submitAttend(attendIds));
    }

    /**
     * 撤回考勤记录
     */
    @RequiresPermissions("record:attend:recall")
    @Log(title = "考勤", businessType = BusinessType.UPDATE)
    @PostMapping( "/recall")
    @ResponseBody
    public AjaxResult recall(String attendIds)
    {
        return toAjax(recordAttendService.recallAttend(attendIds));
    }
}
